package org.stvd.controller.workflow;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.stvd.common.aspectj.lang.annotation.Log;
import org.stvd.common.aspectj.lang.enums.BusinessType;
import org.stvd.common.utils.InterfaceResult;
import org.stvd.common.utils.ServiceResult;
import org.stvd.controller.BaseController;
import org.stvd.core.dto.QueryResult;
import org.stvd.entities.admin.Department;
import org.stvd.entities.workflow.WfGroup;
import org.stvd.entities.workflow.WfUser;
import org.stvd.service.admin.DepartmentService;
import org.stvd.service.workflow.WfGroupService;
import org.stvd.service.workflow.WfUserService;

/**
 * @Title WorkflowGroupController
 * @Destribution 流程审批分组管理类
 * @author wwh
 * @date 2020年10月27日
 */
@Controller
@RequestMapping("/workflow/group")
public class GroupController extends BaseController {
	@Autowired
	private WfGroupService wfGroupService;
	@Autowired
	private WfUserService wfUserService;
	@Autowired
	private DepartmentService departmentService;

	private String tmURL = "/workflow/group/";

	/**
	 * @Destribution 新增流程审批分组
	 * @param WfGroup
	 * @return
	 */
	@GetMapping("add")
	public String addGroupLoad(@ModelAttribute("wfGroup") WfGroup wfGroup) {
		return tmURL + "GroupAdd.html";
	}

	@Log(title = "新增分组信息", businessType = BusinessType.INSERT)
	@PostMapping("add")
	@ResponseBody
	public InterfaceResult<Map<String, Object>> addGroupSubmit(HttpServletRequest request, ModelMap map,
			@Valid @ModelAttribute("wfGroup") WfGroup wfGroup, BindingResult bindingResult) throws Exception {
		InterfaceResult<Map<String, Object>> result = new InterfaceResult<Map<String, Object>>();
		ServiceResult<Map<String, Object>> serviceResult = wfGroupService.insertWfGroup(wfGroup);
		result.transferResult(serviceResult);
		return result;
	}
	
	/**
	 * @Destribution 选择流程审批用户
	 * @param gcode
	 * @param userId
	 * @return
	 */
	@PostMapping("userchoose")
	@ResponseBody
	public InterfaceResult<Map<String, Object>> addUserSubmit(@RequestParam(value = "ids", required = false) String ids,
			@RequestParam(value = "groupCode", required = false) String groupCode) {
		InterfaceResult<Map<String, Object>> result = new InterfaceResult<Map<String, Object>>();
		result.transferResult(wfGroupService.updateUserChooseInfo(ids, groupCode));
		return result;
	}
	
	/**
	 * @Destribution 修改流程审批分组
	 * @param groupId
	 * @param WfGroup
	 * @return
	 */
	@GetMapping("edit/{groupId}")
	public String editGroupLoad(@PathVariable Long groupId, @ModelAttribute("WfGroup") WfGroup wfGroup) {
		wfGroup = wfGroupService.findByPk(WfGroup.class, groupId);
		map.put("wfGroup", wfGroup);
		return tmURL + "GroupEdit.html";
	}

	@Log(title = "修改分组信息", businessType = BusinessType.UPDATE)
	@PostMapping("edit")
	@ResponseBody
	public InterfaceResult<Map<String, Object>> editGroupSubmit(@Valid @ModelAttribute("wfGroup") WfGroup wfGroup, BindingResult bindingResult) {
		InterfaceResult<Map<String, Object>> result = new InterfaceResult<Map<String, Object>>();
		ServiceResult<Map<String, Object>> serviceResult = wfGroupService.updateWfGroup(wfGroup);
		result.transferResult(serviceResult);
		return result;
	}
	
	/**
	 * 删除流程审批分组信息
	 * 
	 * @param groupId 主键
	 * @return
	 */
	@Log(title = "删除分组信息", businessType = BusinessType.DELETE)
	@PostMapping("del")
	@ResponseBody
	public InterfaceResult<Map<String, Object>> deleteGroup() {
		String groupId = request.getParameter("groupId");
		InterfaceResult<Map<String, Object>> result = new InterfaceResult<Map<String, Object>>();
		result.transferResult(wfGroupService.deleteWfGroup(Long.valueOf(groupId)));
		return result;
	}
	
	/**
	 * 流程审批分组信息详情
	 * 
	 * @param groupId 主键
	 * @return
	 */
	@GetMapping("detail/{groupId}")
	public String viewGroup(@PathVariable String groupId) {
		WfGroup wfGroup = wfGroupService.findByPk(WfGroup.class, Long.valueOf(groupId));
		if (wfGroup != null) {
			map.put("wfGroup", wfGroup);
			List<WfUser> userList = wfUserService.listWfUserByCode(wfGroup.getCode());
			map.put("userList", userList);
		}
		return tmURL + "GroupDetail.html";
	}
	
	
	/**
	 * 管理页面
	 * 
	 * @param WfGroup 流程审批分组实体类
	 * @return
	 */
	@RequestMapping("manage")
	public String groupManage(@ModelAttribute("wfGroup") WfGroup wfGroup) {
		//审批分组列表
		QueryResult<WfGroup> queryResult = wfGroupService.wfGroupQuery((pageNo - 1) * pageSize, pageSize,
				wfGroup.getName(), wfGroup.getCode(), "");
		map.put("queryResult", queryResult);
		/***************选择审批用户*******************/
		List<Department> depList = departmentService.findAll(Department.class);
		map.put("depList", depList);
		QueryResult<Map<String, Object>> wfUserqueryResult = wfUserService
				.wfUserQueryResultByName((pageNo - 1) * pageSize, pageSize, "", "");
		map.put("wfUserList", wfUserqueryResult.getRows());
		return tmURL + "GroupManage.html";
	}
	
}
